package com.itcast.gateway.config;

import com.itcast.gateway.handler.SwaggerResourceHandler;
import com.itcast.gateway.handler.SwaggerSecurityHandler;
import com.itcast.gateway.handler.SwaggerUiHandler;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

/**
 * @author zheng.zhang
 * Description 路由配置信息
 * @date 2020/9/11 10:32
 * Version 1.0
 */
@Configuration
@AllArgsConstructor
public class RouterFunctionConfiguration {
    private final SwaggerResourceHandler resourceHandler;
    private final SwaggerSecurityHandler securityHandler;
    private final SwaggerUiHandler uiHandler;

    /**
     * RouterFunction
     * 路由，相当于@RequestMapping，用来判断什么样的url映射到那个具体的HandlerFunction
     * 输入为请求，输出为装在Mono里边的HandlerFunction
     *
     * @return RouterFunction
     */
    @Bean
    public RouterFunction<ServerResponse> routerFunction() {
        return RouterFunctions.route(
                RequestPredicates.GET("/swagger-resources")
                        .and(RequestPredicates.accept(MediaType.ALL)), resourceHandler)
                .andRoute(RequestPredicates.GET("/swagger-resources/configuration/ui")
                        .and(RequestPredicates.accept(MediaType.ALL)), uiHandler)
                .andRoute(RequestPredicates.GET("/swagger-resources/configuration/security")
                        .and(RequestPredicates.accept(MediaType.ALL)), securityHandler);
    }
}
